var layer;
layui.use(['element','form','table','layer','laydate'], function(){
    var $ = layui.jquery
        ,element = layui.element
        ,form = layui.form
        ,table = layui.table;
    util = layui.util;
    var laydate = layui.laydate;
    layer = layui.layer;
    initAkInfo();
    initSyncFrequency();
    initCommunities(); //小区列表

    //自定义表单验证（数据更新频率）
    form.verify({
        validateFrequency: [
            /^[1-9]\d*$/  //正则表达式
            ,'同步频率只能为正整数'  //提示信息
        ]
    });

    function initCommunities(){
        $.get('/acsCommunity/listCommunities', {}, function (result) {
            if(result != null){
                if(result.resultContent != null){
                    var a$opt_html = "";
                    var allOption = "<option value=''>全部</option>";
                    $.each(result.resultContent, function (index, item) {
                        allOption += "<option id='" + item.CODE + "' value='" + item.ID + "'>" + item.NAME + "</option>";
                        a$opt_html += "<option id='" + item.CODE + "' value='" + item.ID + "'>" + item.NAME + "</option>";
                        $("#communityLocation").empty().append(allOption);
                        $("#communityLocationc").empty().append(allOption);
                        $("#communityLocation2").empty().append(a$opt_html);
                        $("#communityLocation3").empty().append(a$opt_html);
                        form.render('select');
                    });

                }
            }
        });
    }

    function listBuildingTree(companyId){
        $.get('/doorDevice/listBuildingTree',{companyId:companyId},function (res) {
            if(res){
                var communityList = res.communityList;
                var buildingMap = res.buildingMap;
                var curr = "";
                for(var i in communityList){
                    curr = communityList[0].id;
                }
                /*var item = "<option value=''>全部</option>";
                var curr = "";
                for(var i in communityList){
                    curr = communityList[0].id;
                    item += "<option value='" + communityList[i].id + "'>" + communityList[i].text + "</option>";
                }
                $("#communityLocation").empty().append(item);
                if(curr){
                    var buildingOptions = "<option value=''>全部</option>";
                    var buildings = buildingMap[curr];
                    for(var i in buildings){
                        buildingOptions += "<option value='" + buildings[i].id + "'>" + buildings[i].text + "</option>";
                    }
                    $("#buildingLocation").empty().append(buildingOptions);
                    $("#areaId").empty().append(buildingOptions);
                }*/
                var buildingOptions = "<option value=''>全部</option>";
                var buildings = buildingMap[curr];
                for(var i in buildings){
                    buildingOptions += "<option value='" + buildings[i].id + "'>" + buildings[i].text + "</option>";
                }
                $("#buildingLocation").empty().append(buildingOptions);
                $("#areaId").empty().append(buildingOptions);
                form.render();
            }
        });
    }

    form.on('select(communityLocation)', function(data){
        // var buildingOptions = "<option value=''>全部</option>";
        // var buildings = buildingMap[data.value];
        // for(var i in buildings){
        //     buildingOptions += "<option value='" + buildings[i].id + "'>" + buildings[i].text + "</option>";
        // }
        // $("#buildingLocation").empty().append(buildingOptions);
        // form.render();
        listBuildingTree(data.value);
    });
    $("#updateData").on("click", function (){
        layer.open({
            type: 1,
            title:"更新开门记录",
            area: ['600px', '260px'],
            content: $('#updateOpenRecord')
        });
    });
    // 更新开门记录
    form.on('submit(updateData)',function(data){
        var companyId = $("#communityLocation2").val();
        if(!companyId){
            layer.msg('请选择要更新门禁记录的小区', {icon: 5});
            return;
        }
        var index = layer.load(2, { //icon0-2 加载中,页面显示不同样式
            shade: [0.4, '#000'], //0.4为透明度 ，#000 为颜色
            content: '更新数据耗时可能较长...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',//图标与样式会重合，这样设置可以错开
                    'width': '200px'//文字显示的宽度
                });
            }
        });
        var startDate = $("#openBeginDate3").val();
        var endDate = $("#openEndDate3").val();
        $.ajax({
            url: "/unAuth/openRecord/updateData",
            type: "post",
            data: {companyId:companyId,startDate:startDate,endDate:endDate},
            success: function (result) {
                layer.close(index);
                if(result.resultCode === '000'){
                    reloadOpenRecord();
                    layer.msg(result.resultDesc, {icon: 1});
                }else{
                    layer.msg(result.resultDesc, {icon: 5});
                }
            }
        });
        return false;
    });
    $("#updateData2").on("click", function (){
        layer.open({
            type: 1,
            title:"更新抓拍记录",
            area: ['600px', '260px'],
            content: $('#updateCaptureRecord')
        });
    });
    // 更新抓拍记录
    form.on('submit(updateData2)',function(data){
        var companyId = $("#communityLocation3").val();
        if(!companyId){
            layer.msg('请选择要更新抓拍记录的小区', {icon: 5});
            return;
        }
        var index = layer.load(2, { //icon0-2 加载中,页面显示不同样式
            shade: [0.6, '#000'], //0.4为透明度 ，#000 为颜色
            content: '更新数据耗时可能较长...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',//图标与样式会重合，这样设置可以错开
                    'width': '200px'//文字显示的宽度
                });
            }
        });
        var startDate = $("#openBeginDate4").val();
        var endDate = $("#openEndDate4").val();
        $.ajax({
            url: "/captureRecord/updateData",
            type: "post",
            data: {companyId:companyId,startDate:startDate,endDate:endDate},
            success: function (result) {
                layer.close(index);
                if(result.resultCode === '000'){
                    reloadCandidPhoto();
                    layer.msg(result.resultDesc, {icon: 1});
                }else{
                    layer.msg(result.resultDesc, {icon: 5});
                }
            }
        });
        return false;
    });

    //执行一个laydate实例
    beginDateInit = function(startId,endId ){
        laydate.render({
            elem: startId,
            type: 'datetime',
            done: function (value ) {
                var startDate = new Date(value).getTime();
                var endTime = new Date($(endId).val()).getTime();
                if (endTime < startDate) {
                    layer.msg('结束时间不能小于开始时间');
                    $(startId).val('');
                }
            }
        })
    }
    endDateInit = function(startId,endId ){
        laydate.render({
            elem: endId,
            type: 'datetime',
            done: function (value ) {
                var startDate = new Date($(startId).val()).getTime();
                var endTime = new Date(value).getTime();
                if (endTime < startDate) {
                    layer.msg('结束时间不能小于开始时间');
                    $(endId).val('');
                }
            }
        })
    }

    // 开门记录时间
    beginDateInit('#openBeginDate','#openEndDate');
    endDateInit('#openBeginDate','#openEndDate');
    // 抓拍时间
    beginDateInit('#openBeginDate2','#openEndDate2');
    endDateInit('#openBeginDate2','#openEndDate2');
    // 更新门禁时间
    beginDateInit('#openBeginDate3','#openEndDate3');
    endDateInit('#openBeginDate3','#openEndDate3');
    // 更新抓拍时间
    beginDateInit('#openBeginDate4','#openEndDate4');
    endDateInit('#openBeginDate4','#openEndDate4');
    //通话记录呼叫时间
    // beginDateInit('#callBeginDate','#callEndDate' );
    // endDateInit('#callBeginDate','#callEndDate' );
    //通话记录接听时间
    // beginDateInit('#listenBeginDate','#listenEndDate' );
    // endDateInit('#listenBeginDate','#listenEndDate' );
    //二维码记录时间
    // beginDateInit('#qrcodeBeginDate','#qrcodeEndDate' );
    // endDateInit('#qrcodeBeginDate','#qrcodeEndDate' );
    //随机数字时间
    // beginDateInit('#randNumBeginDate','#randNumEndDate' );
    // endDateInit('#randNumBeginDate','#randNumEndDate' );
    //人脸记录时间
    // beginDateInit('#upFaceBeginDate','#upFaceEndDate' );
    // endDateInit('#upFaceBeginDate','#upFaceEndDate' );

    // $.get('/accessDevice/getAllDeviceInfo',function (res) {
    //     var data = res.resultContent;
    //     var item = '<option value=""></option>';
    //     for(i in data){
    //         item += "<option value='" + data[i].deviceName + "'>" + data[i].deviceName + "</option>";
    //     }
    //     $("#selAllDeviceOpen").append(item);
    //     $("#selAllDeviceCall").append(item);
    //     $("#selAllDeviceListen").append(item);
    //     form.render();
    // });
    //

    //重置开门记录表
    reloadOpenRecord = function () {
        document.getElementById("openForm").reset();
        table.reload('openTable',{
            where:{"areaId":"","wyUserType":"","gateLocakName":$("#gateLocakName").val(),
                "startDate":"","endDate":"","companyId":$("#communityLocation").val()}
        });
    };
    // 重置抓拍记录
    resetCandidPhoto = function () {
        document.getElementById("candidPhotoForm").reset();
        layui.form.render();
        table.reload('candidPhotoTable',{
            where:{"lockName":'',
                "startDate":'',"endDate":''}
        });
    };
    //重置通话记录表
    reloadCall = function () {
        document.getElementById("callForm").reset();
        layui.form.render();
        table.reload('callTable',{
            where:{recordType:"2", deviceMainBody:'',beginTime:'',endTime:'',listenBeginTime:'',
                listenEndTime:'',deviceName:'' }
        });
    };
    //重置二维码生成表
    reloadQrcode = function () {
        document.getElementById("qrcodeForm").reset();
        table.reload('qrcodeTable',{
            where:{recordType:"3" , operatorName:'',beginTime:'',endTime:''}
        });
    };
    //重置随机数字生成表
    reloadrandNum = function () {
        document.getElementById("randNumForm").reset();
        table.reload('randNumTable',{
            where:{recordType:"4" , operatorName:'',beginTime:'',endTime:''}
        });
    };
    //重置人脸上传生成表
    reloadupFace = function () {
        document.getElementById("upFaceForm").reset();
        table.reload('upFaceTable',{
            where:{recordType:"5" , operatorName:'',beginTime:'',endTime:''}
        });
    };

    //开门记录表
    table.render({
        elem: '#openTable',
        height: 'full-150',
        url:'/unAuth/openRecord/list',
        // toolbar: '#toolbarDemo',
        method: 'get',
        where:{},
        page: true,
        cols: [[
            {type:'numbers',title:'序号'}
            ,{field: 'gateLocakName', title: '门禁', align:'center'}
            ,{field: 'wyUserName', title: '开门用户', align:'center'}
            ,{field: 'wyUserType', title: '用户类型', align:'center',templet:function (d) {
                    return parseUserType(d.wyUserType);
                }}
            ,{field: 'openType', title: '开门类型', align:'center',templet:function (d) {
                    return parseOpenType(d.openType);
                }}
            // ,{field: 'ocardNo', title: '卡号', align:'center'}
            ,{field: 'createTime', title: '开门时间', align:'center',templet: function (d) {
                    // return formatTime(d.createTime,"yyyy-MM-dd hh:mm:ss");
                    return util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss");
                }}
            ,{field: 'completePicUrl', title: '主摄像头开门图片', align:'center',templet:function (d) {
                    if(d.completePicUrl){
                        // return '<a href="'+d.completePicUrl+'" target="_blank">查看</a>';
                        return '<img onclick="magnifyPic(this)" style="cursor: pointer" width="30" height="25" src="'+d.completePicUrl+'"/>';
                    }
                    return "";
                }}
            ,{field: 'completeAssistantPicUrl', title: '辅助摄像头开门图片', align:'center',templet:function (d) {
                    if(d.completeAssistantPicUrl){
                        // return '<a href="'+d.completeAssistantPicUrl+'" target="_blank">查看</a>';
                        return '<img onclick="magnifyPic(this)" style="cursor: pointer" width="30" height="25" src="'+d.completeAssistantPicUrl+'"/>';
                    }
                    return "";
                }}
        ]]
    });

    //抓拍记录表
    table.render({
        elem: '#candidPhotoTable',
        height: 520,
        url:'/captureRecord/list',
        method: 'get',
        where:{},
        page: true,
        cols: [[
            {type:'numbers',title:'序号'}
            ,{field: 'lockName', title: '门禁名称', align:'center'}
            ,{field: 'imgUrl', title: '抓拍图片', align:'center',templet:function (d) {
                if(d.imgUrl){
                    return '<img onclick="magnifyPic(this)" style="cursor: pointer" width="30" height="25" src="'+d.imgUrl+'"/>';
                    // return '<a href="'+d.imgUrl+'" target="_blank">查看</a>';
                }
                return "";
            }}
            ,{field: 'captureTime', title: '抓拍时间', align:'center',templet: function (d) {
                return formatTime(d.captureTime,"yyyy-MM-dd hh:mm:ss");
            }}
        ]]
    });
    //通话记录表
    /*table.render({
        elem: '#callTable',
        height: 520,
        url:'/acsControlRecordInfo/getTable',
        // toolbar: '#toolbarDemo',
        method: 'get',
        where:{recordType:"2"},
        page: true, //开启分页
        cols: [[ //表头
            {field: 'acsRecordId', title: '序号', align:'center'}
            ,{field: 'deviceName', title: '呼叫设备', align:'center'}
            ,{field: 'createTime', title: '呼叫时间', align:'center',templet: function (d) {
                    return util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss");
                }    }
            ,{field: 'deviceMainBody', title: '接听设备',sort: true, align:'center'}
            ,{field: 'listenedTime', title: '接听时间', sort: true,align:'center',templet: function (d) {
                    if(d.listenedTime === null || d.listenedTime==="" ){
                        return "";
                    }
                return util.toDateString(d.listenedTime, "yyyy-MM-dd HH:mm:ss");
                }    }
            ,{field: 'taikDuration', title: '通话时长', sort: true,align:'center'}
        ]]
    });*/
    //二维码记录表
    table.render({
        elem: '#qrcodeTable',
        height: 520,
        url:'/acsControlRecordInfo/getTable',
        // toolbar: '#toolbarDemo',
        method: 'get',
        where:{recordType:"3"},
        page: true, //开启分页
        cols: [[ //表头
            {field: 'acsRecordId', title: '序号', align:'center'}
            ,{field: 'createTime', title: '操作时间', align:'center',templet: function (d) {
                    return util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss");
                }    }
            ,{field: 'recordInfo', title: '随机二维码',sort: true, align:'center' }
            ,{field: 'visitorName', title: '访客姓名', sort: true,align:'center'}
            ,{field: 'operatorName', title: '操作用户', sort: true,align:'center'}
            ,{field: 'state', title: '到访状态', align:'center',templet: function (d) {
                    return vstate(d.state );
                }}
            ,{field: 'listenedTime', title: '有效时间', align:'center',templet: function (d) {
                    return util.toDateString(d.listenedTime, "yyyy-MM-dd HH:mm:ss");
                }    }
        ]],
        done: function (res, curr, count) {
            // tableList = res.data;
            //    $('th').css({'background-color': '#003eee','color':' #fff','font-weight':'bold'})
            var that = this.elem.next();
            res.data.forEach(function (item, index) {
                console.log(item);
                if(item.recordInfo !=null && item.recordInfo !== ''   ){
                    var tr = that.find(".layui-table-box tbody tr[data-index=" + index + "]");
                    if (index % 2 === 0) {
                        tr.css('background-color', '#fff');
                    } else {
                        tr.css('background-color', 'rgba(112,175,253,.1)');
                    }
                    var td = tr.find('.laytable-cell-3-0-2');
                    console.log(td);
                    $(td).empty();
                    $(td).qrcode({
                        render: "canvas",
                        width: 50,
                        height: 50,
                        foreground: "#000",
                        background: "#fff",
                        text: item.recordInfo
                    });
                    var flag = true;
                    function draw(w,b){
                        $(td).empty();
                        $(td).qrcode({
                            render: "canvas",
                            width: w,
                            height: w,
                            foreground: "#000",
                            background: "#fff",
                            text: item.recordInfo
                        });
                        $(td).css({
                            'transform': 'translateX('+b+'px)',
                            'transition':' all 0.1s ease-in'
                        })
                    }
                    $(td).click(function () {
                        if (flag) {
                            draw(100,-20)
                        } else {
                            draw(50,0)
                        }
                        flag = !flag
                    })
                }
            })
        }
    });
    //随机数字记录表
    table.render({
        elem: '#randNumTable',
        height: 520,
        url:'/acsControlRecordInfo/getTable',
        // toolbar: '#toolbarDemo',
        method: 'get',
        where:{recordType:"4"},
        page: true, //开启分页
        cols: [[ //表头
            {field: 'acsRecordId', title: '序号', align:'center'}
            ,{field: 'createTime', title: '操作时间', align:'center',templet: function (d) {
                    return util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss");
                }    }
            ,{field: 'recordInfo', title: '随机数字',sort: true, align:'center' }
            ,{field: 'visitorName', title: '访客姓名', sort: true,align:'center'}
            ,{field: 'operatorName', title: '操作用户', sort: true,align:'center'}
            ,{field: 'state', title: '到访状态', align:'center',templet: function (d) {
                    return vstate(d.state );
                }}
            ,{field: 'listenedTime', title: '有效时间', align:'center',templet: function (d) {
                    return util.toDateString(d.listenedTime, "yyyy-MM-dd HH:mm:ss");
                }    }
        ]]
    });
    //人脸上传记录表
    // table.render({
    //     elem: '#upFaceTable',
    //     height: 520,
    //     url:'/acsControlRecordInfo/getTable',
    //     // toolbar: '#toolbarDemo',
    //     method: 'get',
    //     where:{recordType:"5"},
    //     page: true, //开启分页
    //     cols: [[ //表头
    //         {field: 'acsRecordId', title: '序号', align:'center'}
    //         ,{field: 'createTime', title: '操作时间', align:'center',templet: function (d) {
    //                 return util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss");
    //             }    }
    //         ,{field: 'recordInfo', title: '人脸信息',sort: true, align:'center',templet: function (d) {
    //             if(d.recordInfo === null || d.recordInfo==="" ){
    //                 return "";
    //             }
    //             return "<div><img class='layui-nav-img' src='data:image/jpeg;base64," + selVisitorImg(d.recordInfo) + "'></div>";
    //         }}
    //         ,{field: 'state', title: '到访状态', align:'center',templet: function (d) {
    //                 return vstate(d.state );
    //         }}
    //         ,{field: 'listenedTime', title: '有效时间', align:'center',templet: function (d) {
    //                 return util.toDateString(d.listenedTime, "yyyy-MM-dd HH:mm:ss");
    //             }    }
    //         ,{field: 'visitorName', title: '访客姓名', sort: true,align:'center'}
    //         ,{field: 'operatorName', title: '操作用户', sort: true,align:'center'}
    //     ]]
    // });

    // 开门记录查询
    form.on('submit(openQuery)',function () {
        reloadOne();
        return false;
    });
    // 抓拍记录
    form.on('submit(candidPhotoQuery)',function () {
        reloadCandidPhoto();
        return false;
    });
    function reloadCandidPhoto() {
        var companyId = $("#communityLocationc").val();
        var lockName= $.trim($("#gateLocakName2").val());
        var startDate= $("#openBeginDate2").val();
        var endDate= $("#openEndDate2").val();
        table.reload('candidPhotoTable',{
            where:{"companyId":companyId,"lockName":lockName,
                "startDate":startDate,"endDate":endDate}
        });
    }
    function reloadOne() {
        var lockName= $.trim($("#gateLocakName").val());
        var areaId= $("#buildingLocation").val();
        var companyId= $("#communityLocation").val();
        var userType= $("#userType").val();
        var startDate= $("#openBeginDate").val();
        var endDate= $("#openEndDate").val();
        table.reload('openTable',{
            where:{"areaId":areaId,"wyUserType":userType, "gateLocakName":lockName,
                "startDate":startDate,"endDate":endDate,"companyId":companyId}
        });
    }
    //通话记录查询
    form.on('submit(callQuery)',function () {
        var deviceName = $("#selAllDeviceCall").val();
        var deviceMainBody = $("#selAllDeviceListen").val();
        var beginTime = $("#callBeginDate").val();
        var endTime = $("#callEndDate").val();
        var listenBeginTime = $("#listenBeginDate").val();
        var listenEndTime = $("#listenEndDate").val();
        table.reload('callTable',{
            where:{recordType:"2", deviceMainBody:deviceMainBody,beginTime:beginTime,endTime:endTime,listenBeginTime:listenBeginTime,
                listenEndTime:listenEndTime,deviceName:deviceName }
        });
        return false;
    });
    //二维码记录查询
    form.on('submit(qrcodeQuery)',function () {
        var operatorName = $("#opUserName").val();
        var beginTime = $("#qrcodeBeginDate").val();
        var endTime = $("#qrcodeEndDate").val();
        table.reload('qrcodeTable',{
            where:{recordType:"3", operatorName:operatorName,beginTime:beginTime,endTime:endTime}
        });
        return false;
    });
    //随机数字记录查询
    form.on('submit(randNumQuery)',function () {
        var operatorName = $("#randNumUserName").val();
        var beginTime = $("#randNumBeginDate").val();
        var endTime = $("#randNumEndDate").val();
        table.reload('randNumTable',{
            where:{recordType:"4", operatorName:operatorName,beginTime:beginTime,endTime:endTime}
        });
        return false;
    });
    //上传人脸记录查询
    form.on('submit(upFaceQuery)',function () {
        var operatorName = $("#upFaceUserName").val();
        var beginTime = $("#upFaceBeginDate").val();
        var endTime = $("#upFaceEndDate").val();
        table.reload('upFaceTable',{
            where:{recordType:"5", operatorName:operatorName,beginTime:beginTime,endTime:endTime}
        });
        return false;
    });

    function initAkInfo() {
        $.get('/unAuth/openRecord/getAkInfoByUser', {}, function (result) {
            if(result != null){
                if(result.resultContent != null){
                    var akInfo = result.resultContent;
                    setAkInfo(akInfo);
                }
            }
        });
    }
    function initSyncFrequency() {
        $.get('/unAuth/openRecord/getSyncfFrequency', {}, function (result) {
            if(result != null){
                if(result.resultContent != null){
                    var syncFre = result.resultContent;
                    setSyncFre(syncFre);
                }
            }
        });
    }
    function setSyncFre(syncFre){
        if(syncFre){
            $("#openSyncFre").val(syncFre.openSyncFre);
            $("#capSyncFre").val(syncFre.capSyncFre);
        }
    }
    function setAkInfo(akInfo){
        if(akInfo){
            $("#appid").val(akInfo.appid);
            $("#appsecret").val(akInfo.appsecret);
            $("#phone").val(akInfo.phone);
        }
    }
    // 保存配置信息
    form.on('submit(saveOrUpdate)',function(data){
        $.post("/unAuth/openRecord/saveAkInfo",data.field, function(result) {
            if(result.resultCode === '000' ){
                layer.msg('保存成功', {icon: 1});
                setAkInfo(result.resultContent);
            }else{
                layer.msg(result.resultDesc, {icon: 0});
            }
        });
        return false;
    });

    form.on('submit(saveOrUpdateFre)',function(data){
        $.post("/unAuth/openRecord/saveSyncFre",data.field, function(result) {
            if(result.resultCode === '000' ){
                layer.msg('保存成功', {icon: 1});
                setSyncFre(result.resultContent);
            }else{
                layer.msg(result.resultDesc, {icon: 0});
            }
        });
        return false;
    });

    selVisitorImg = function (vid ) {
        var imgBase64 = '';
        $.ajax({
            url:'/acsControlImageInfo/selectOne',
            type: 'get',
            data: {id: vid },
            async: false,
            success:function(res){
                 if(res){
                     imgBase64 = res.imageInfo;
                 }
            }
        });
        return imgBase64;
    }

    function vstate(state) {
        switch (state) {
            case '1':
                return "未访问";
            case '2':
                return "已到访";
            case '11':
                return "未访问";
            case '21':
                return "已到访";
        }
    }

});
function parseUserType(type) {
    switch (type) {
        case 11: return '业主';
        case 12: return '家庭成员';
        case 13: return '租客';
        default: return '-';
    }
}
function parseOpenType(type) {
    switch (type) {
        case 1: return 'APP开门';
        case 2: return '刷卡开门';
        case 3: return '密码开门';
        case 4: return '视频开门';
        case 6: return '电话开门';
        case 7: return '室内机开门';
        case 8: return '刷脸开门';
        default: return '-';
    }
}

function formatTime(datetime,fmt) {
    if (parseInt(datetime)==datetime) {
        if (datetime.length==10) {
            datetime=parseInt(datetime)*1000;
        } else if(datetime.length==13) {
            datetime=parseInt(datetime);
        }
    }
    datetime=new Date(datetime);
    var o = {
        "M+" : datetime.getMonth()+1,                 //月份
        "d+" : datetime.getDate(),                    //日
        "h+" : datetime.getHours(),                   //小时
        "m+" : datetime.getMinutes(),                 //分
        "s+" : datetime.getSeconds(),                 //秒
        "q+" : Math.floor((datetime.getMonth()+3)/3), //季度
        "S"  : datetime.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt))
        fmt=fmt.replace(RegExp.$1, (datetime.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
        if(new RegExp("("+ k +")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
    return fmt;
}

function magnifyPic(ele) {
    var width = $(ele).width();
    var height = $(ele).height();
    var scaleWH = width/height;
    var bigH = 450;
    var bigW = scaleWH*bigH;
    if(bigW>600){
        bigW = 600;
        bigH = bigW/scaleWH;
    }
    // 放大预览图片
    layer.open({
        type: 1,
        title: false,
        closeBtn: 1,
        shadeClose: true,
        area: [bigW + 'px', bigH + 'px'], //宽高
        content: "<img width='"+bigW+"' height='"+bigH+"' src=" + $(ele).attr("src") + " />"
    });
}